package com.xxx.video.mine.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.mine.activitys.ChangeMobileOrEmailActivity;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;


public class ChangeMobileOrEmailPresenter extends BasePresenter<IChangeMobileOrEmailView> {

    public ChangeMobileOrEmailPresenter(@NonNull Context context, IChangeMobileOrEmailView baseView) {
        super(context, baseView);
    }

    /**
     * save user info
     *
     * @param type type
     */
    public void saveUserInfo(int type) {
        String content = getBaseView() == null ? "" : getBaseView().getEmailOrMobile();
        String mobile = type == ChangeMobileOrEmailActivity.CHANGE_MOBILE ? content : "";
        String email = type == ChangeMobileOrEmailActivity.CHANGE_EMAIL ? content : "";
        String nickName = type == ChangeMobileOrEmailActivity.CHANGE_NICK_NAME ? content : "";

        if (type == ChangeMobileOrEmailActivity.CHANGE_MOBILE && TextUtils.isEmpty(content)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_change_mobile_can_not_empty));
            return;
        } else if (type == ChangeMobileOrEmailActivity.CHANGE_EMAIL && TextUtils.isEmpty(content)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_change_email_can_not_empty)+StringUtils.getInstance().getString(R.string.tv_account_information_email_title));
            return;
        } else if (type == ChangeMobileOrEmailActivity.CHANGE_NICK_NAME && TextUtils.isEmpty(nickName)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_change_nick_name_can_not_empty));
            return;
        }
        showProgressDialog("loading");
        NetWorkUtils.getInstance().updateInfo(mobile, nickName, email, "", "", "", new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().upDateUserInfoSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
            }
        });

    }
}
